<template>
  <div class="mod-config">
    <el-form inline @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.returnBillId"></el-input>
      </el-form-item>
      <el-form-item label="运单单号">
        <el-input v-model="dataForm.transportId"></el-input>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.createUsername"></el-input>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName" placeholder="请选择">
          <el-button slot="append" @click="chooseSupply">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="退货部门">
        <el-input v-model="dataForm.deptId"></el-input>
      </el-form-item>
      <el-form-item label="发出仓库">
        <el-input v-model="dataForm.outStockId"></el-input>
      </el-form-item>
      <el-form-item label="结算人">
        <el-input v-model="dataForm.settlementUsername"></el-input>
      </el-form-item>
      <el-form-item label="结算状态">
        <el-select v-model="dataForm.settlementStatus">
          <el-option v-for="item in settlementStatusOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结算日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.settlementTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.settlementTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-select v-model="dataForm.ctgId">
          <el-option v-for="item in ctgOptions" :value="item.value" :label="item.name" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" plain @click="searchHandle">查询</el-button>
        <el-button icon="el-icon-search" type="info" plain @click="clearSearchForm">清除</el-button>
      </el-form-item>
    </el-form>

    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading">
        <el-table-column prop="returnBillId" label="单据编号" width="188"></el-table-column>
        <el-table-column prop="transportId" label="运单单号"></el-table-column>
        <el-table-column prop="supplyName" label="制造商简称" width="150"></el-table-column>
        <el-table-column prop="outStockName" label="发出仓位" width="80"></el-table-column>
        <el-table-column prop="settlementStatus" label="结算状态" :formatter="settlementStatusFormatter"></el-table-column>
        <el-table-column prop="createUsername" label="制单人"></el-table-column>
        <el-table-column prop="createTime" label="单据日期" width="168"></el-table-column>
        <el-table-column prop="settlementUsername" label="结算人"></el-table-column>
        <el-table-column prop="settlementTime" label="结算日期"></el-table-column>
        <el-table-column  label="操作" fixed="right" width="99">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-collection" @click="returnGoodsDetail(scope.row.id)">详情</el-button>
            <el-button v-if="permissions.eims_settlement_edit" :disabled="scope.row.settlementStatus == 1" type="text" icon="el-icon-edit" @click="editSettlementInfo(scope.row.id)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
    <detail-dialog v-if="detailDialogVisible" ref="detailDialog"></detail-dialog>
    <table-form v-if="editSettlementVisible" ref="editSettlement" @refreshDataList="getDataList"></table-form>
    <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
  </div>
</template>

<script>
  import { filterParams } from "@/util/tools"
  import { returnSettlementFetchList } from "@/api/eims/return-management"
  import { mapGetters } from "vuex"
  import { getCtgDataList } from "@/api/eims/check"

	import SupplySearch from '@/components/business/SearchSupply'
	import DetailDialog from "./detail"
	import TableForm from "./form"
  export default {
    data() {
      return {
        dataForm: {
          supplyName: ''
        },
        dataList: [],
        settlementStatusOptions: [
          { label: '已审核', value: 1 },
          { label: '未审核', value: 0 }
        ],
        ctgOptions: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        editSettlementVisible: false,
        detailDialogVisible: false,
        supplyVisible: false,
      };
    },
    components: {
      DetailDialog,
      TableForm,
      SupplySearch
    },
    created() {
      this.getDataList();
      this.getGoodsCtgList();
    },
    computed: {
      ...mapGetters(["permissions"])
    },
    methods: {
      //	查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          this.dataForm[key] = "";
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        let params2 = filterParams(this.dataForm);
        this.dataListLoading = true;
        returnSettlementFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      // 查看详情
      returnGoodsDetail(id) {
        this.detailDialogVisible = true;
        this.$nextTick(() => {
          this.$refs.detailDialog.init(id);
        });
      },
      // 修改
      editSettlementInfo(id) {
        this.editSettlementVisible = true;
        this.$nextTick(() => {
          this.$refs.editSettlement.init(id);
        });
      },
      settlementStatusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '1':
            return '已审核'
            break
          case '0':
            return '未审核'
            break
        }
      },
      //获取制造商列表
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init()
        })
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      //获取商品类别
      getGoodsCtgList() {
        getCtgDataList().then(response => {
          if(response.data.code == '0'){
            let list = response.data.data.records
            let options = []
            list.forEach(item => {
              options.push({
                value: item.id,
                name: item.name
              })
            })
            this.ctgOptions = options
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>
